<?php

require_once '../library/Utility/Converter.php'; //dung chuyen doi giua doi tuong va mang (object to array || array to object)

class End_Models_MultimediaFileSetAssocs extends Zend_Db_Table_Abstract {

    protected $_name = 'multimedia_file_set_assoc'; // chi ra chinh xac table nao
    protected $_primary = 'file_id'; // chi ra khoa chinh
    
	public function getMetadata()
	{
		if(empty($this->_metadata))
		{
			$this->_setupMetadata();
		}
		return $this->_metadata;
	}
	
    public function _find($file_id) 
    {
        try 
        {
            $row = parent::find($file_id)->current();
            return Utility_Converter::arrayToObject($row->toArray());
        }catch(Zend_Db_Exception $ex) 
        {
            throw $ex;
        }
    }

    public function _filter($stm, $value, $count = null, $offset = null) {
        try {

            $select =  parent::select();

            if(is_array($stm) && is_array($value)) 
            {
                for($i = 0; $i < count($stm); $i++) 
                {
                    $select->where($stm[$i],  $value[$i]);
                }
            }
            else 
            {
                $select->where($stm,  $value);
            }
            
            if($count != NULL && $offset != NULL)
            {
                $select->order('id ASC')
                       ->limit($count, $offset);
            }
            $rows = parent::fetchAll($select);
            $objects = array();
            
            for($i = 0; $i < count($rows); $i++) 
            {
                $objects[$i] = Utility_Converter::arrayToObject($rows[$i]->toArray());
            }
            return $objects;
            
        }catch(Zend_Db_Exception $ex) 
        {
            throw $ex;
        }
    }


    public function _update($object) 
    {
        try 
        {
            $arrData = Utility_Converter::objectToArray($object);
            $adapter = parent::getAdapter();
            $where = $adapter->quoteInto('file_id = ?', $object->file_id); //cap nhat theo khoa 
            parent::update($arrData, $where);
            
        }catch(Zend_Db_Exception $ex) 
        {
            throw $ex;
        }
    }

    public function _insert($object) 
    {
        try 
        {
            $object->file_id = null;
            $arrData = Utility_Converter::objectToArray($object);
            return parent::insert($arrData);
            
        }catch(Zend_Db_Exception $ex) 
        {
            throw $ex;
        }
    }

    public function _delete($file_id) 
    {
        try 
        {
            $where = parent::getAdapter()->quoteInto('file_id = ?', $file_id); //xoa theo khoa
            return parent::delete($where);
                       
        }catch(Zend_Db_Exception $ex) 
        {
            throw $ex;
        }
    }


}

